﻿/*
 * 功能：工厂管理方法类
 * 开发：王亮
 * 时间：2010-02-17
 */
#region namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

namespace Common
{
    public class Factory
    {
        /// <summary>
        /// 引用参数集
        /// </summary>
        private System.Collections.Generic.IList<object> refObj = new System.Collections.Generic.List<object>();

        /// <summary>
        /// 初始化工厂对象
        /// </summary>
        public static Factory NewInstance
        {
            get
            {
                return new Factory();
            }
        }

        public T CallData<T>(object target, string methodname, params object[] paras)
        {
            return (T)GetDataByPractise(target, methodname, paras);
        }

        private object GetDataByPractise(object target, string methodname, params object[] paras)
        {
            object result = null;
            if (paras.Length > 0)
            {
                System.Reflection.ParameterModifier modifer = new System.Reflection.ParameterModifier(paras.Length);
                for (int i = 0; i < paras.Length; i++)
                {
                    if (paras[i].GetType().Equals(typeof(ReferenceObject)))
                    {
                        paras[i] = ((ReferenceObject)paras[i]).Value;
                        modifer[i] = true;
                    }
                }

                System.Reflection.ParameterModifier[] p = { modifer };
                result = target.GetType().InvokeMember(methodname, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, target, paras, p, null, null);
                for (int i = 0; i < paras.Length; i++)
                {
                    if (modifer[i])
                    {
                        refObj.Add(paras[i]);
                    }
                }
            }
            else
            {
                result = target.GetType().InvokeMember(methodname, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null, target, paras);
            }
            return result;
        }

        /// <summary>
        /// 获取引用型参数返回后对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="index"></param>
        /// <returns></returns>
        public T GetReferencePara<T>(int index)
        {
            return (T)this.refObj[index];
        }

        /// <summary>
        /// 引用型参数装饰类
        /// </summary>
        public class ReferenceObject
        {
            object value = new object();
            public object Value
            {
                get { return this.value; }
                set { this.value = value; }
            }

            public ReferenceObject(object value)
            {
                this.Value = value;
            }
        }
    }
}
